package bank;

/**
A checking account that charges transaction fees.
*/
public class CheckingAccount extends BankAccount
{
	private static final int FREE_TRANSACTIONS = 3;
	private static final double TRANSACTION_FEE = 2.0;
	
	private int transactionCount;
	
	/**
	Constructs a checking account with a given balance.
	@param initialBalance the initial balance
	*/
	public CheckingAccount(int accountNumber, double initialBalance)
	{
		// Construct superclass
		super(accountNumber, initialBalance);
		
		// Initialize transaction count
		transactionCount = 0;
	}
	
	/**
    Withdraws money from the checking account.
    @param amount The amount to withdraw
    */
	public void withdraw(double amount)
	{
		if (this.isOpen() && amount > 0 && amount <= this.getBalance())
		{
			super.withdraw(amount);
			transactionCount++;
		}
	}

	/**
	Deducts the accumulated fees and resets the
	transaction count.
	*/
	public void deductFees()
	{
		if (transactionCount > FREE_TRANSACTIONS)
		{
			double fees = TRANSACTION_FEE * (transactionCount - FREE_TRANSACTIONS);
			if (fees > this.getBalance())
			{
				super.withdraw(this.getBalance());
			}
			else
			{
				super.withdraw(fees);
			}
		}
		transactionCount = 0;
	}
}